//
//  GetFollowerListRequest.m
//  collection
//
//  Created by Jing on 4/12/12.
//  Copyright (c) 2012 Tsinghua University. All rights reserved.
//

#import "GetFollowerListRequest.h"
#import "UserInfo.h"

@implementation GetFollowerListRequest

- (void)initParams {
    
    self.descriptionName = @"relation.getfollowers";
    
    GetFollowerListRequestData* requestData = [[GetFollowerListRequestData alloc] init];
    self.request = requestData;
    [requestData release];
    
    GetFollowerListResponse* response = [[GetFollowerListResponse alloc] init];
    self.response = response;
    [response release];
}

- (NSString*)buildRequestURL {
    
    GetFollowerListRequestData* requestData = (GetFollowerListRequestData*)self.request;
    
    NSString* requestContent = [NSString stringWithFormat:@"&uid=%lld&pageNumber=%d",
                                requestData.qUID,
                                requestData.index];
    
    return requestContent;
}

- (void)parseResponse:(NSObject *)data {
    
    GetFollowerListResponse* response = (GetFollowerListResponse*)self.response;
    
    NSDictionary* dic = (NSDictionary*)data;
    // Total num
    NSString* numStr = [dic objectForKey:@"totalNum"];
    if (numStr != nil && ![numStr isEqualToString:@""]) {
        response.totalNum = numStr.longLongValue;
    }
    
    // users
    NSArray* rawUserArray = [dic objectForKey:@"users"];
    if (rawUserArray != nil) {
        
        NSMutableArray* userArray = [[NSMutableArray alloc] initWithCapacity:1];
        for (NSDictionary* userData in rawUserArray) {
            UserInfo* user = [self parseOneUser:userData];
            if (user != nil) {
                [userArray addObject:user];
                [user release];
            }
        }
        
        response.followerList = userArray;
        [userArray release];
    }
}

@end
